package com.woniuxy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.Dto.TopicArrayIdDTo;
import com.woniuxy.Dto.TopicPage;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.TopicTable;
import com.woniuxy.service.TopicDaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * description: TopicController <br>
 * date: 2022/5/31 5:54 下午 <br>
 * author: shesaifei <br>
 * 提交测试
 * version: 1.0 <br>
 */
@RestController
@RequestMapping("/topic")
public class TopicController {
    @Autowired
    private TopicDaoService topicDaoService;

    @GetMapping("/list/{currentPage}/{PageSize}")
    public ResponseEntity findAllTopic(@PathVariable int currentPage, @PathVariable int PageSize) {
        PageHelper.startPage(currentPage, PageSize);
        List<TopicTable> topicList = topicDaoService.findAllTopic();
        PageInfo<TopicTable> topicTablePageInfo = new PageInfo<>(topicList);
        if (topicList != null) {
            return new ResponseEntity(200, "查询成功", topicTablePageInfo);
        } else {
            return new ResponseEntity(500, "查询成功", null);
        }
    }

    //分页加搜索条件
    @RequestMapping("/list")
    public ResponseEntity findAllTopicPage(@RequestBody TopicPage topicPage) {
        PageHelper.startPage(topicPage.getPage(), topicPage.getLimit());
        List<TopicTable> topicList = topicDaoService.findAllTopicPage(topicPage);
        PageInfo<TopicTable> topicTablePageInfo = new PageInfo<>(topicList);
        if (topicList != null) {
            return new ResponseEntity(200, "查询成功", topicTablePageInfo);
        } else {
            return new ResponseEntity(500, "查询成功", null);
        }
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity deleteTopicById(@PathVariable Integer id) {
        int i = topicDaoService.deleteTopicById(id);
        if (i > 0) {
            return ResponseEntity.SUCCESS;
        } else {
            return ResponseEntity.ERROR;
        }
    }

    @DeleteMapping("/deleteAll")//根据题目ID批量删除题目
    public ResponseEntity deleteTopics(@RequestBody TopicArrayIdDTo topicArrayIdDTo) {
        int i = topicDaoService.deleteTopics(topicArrayIdDTo);
        if (i > 0) {
            return ResponseEntity.SUCCESS;
        } else {
            return ResponseEntity.ERROR;
        }
    }

    @PutMapping("/updateTopic")//根据id修改题目信息
    public ResponseEntity updateTopic(@RequestBody TopicTable topicTable) {
        int i = topicDaoService.updateTopic(topicTable);
        if (i > 0) {
            return new ResponseEntity(200, "跟新题目成功！", i);
        } else {
            return new ResponseEntity(500, "跟新题目失败", null);
        }
    }

    @GetMapping("findTopicById/{topicId}")
    public ResponseEntity findTopicById(@PathVariable("topicId") Integer topicId) {
        List<TopicTable> topicTableList = topicDaoService.findTopicById(topicId);
        if (topicTableList != null) {
            return new ResponseEntity(200, "查询成功！", topicTableList);
        } else {
            return new ResponseEntity(500, "查询失败！", null);
        }
    }
}
